<?php 
include_once "../../Lib/DBMySql.inc";
include_once "../../Lib/Common.php";
include_once "../../Entities/pagepositionsObj.php";
include_once "../../DAL/pagepositionsDAL.php";
include_once "../../Lib/Content.php";
include_once "../../Lib/Session.php";
include_once "../header.php";

try
{
	$mainDB = NewDB(DATABASE_HOST, DATABASE_NAME , DATABASE_USER, DATABASE_PASSWORD);
	$title		= "Web Page Position Listing";
	$pageError	= "";
	$frmUpdate 	= "detail.php";	// this page user for add/edit information
	$frmName 	= "frmMain";		// this is main form's name
	// Delete the selected records
	if (isset($act) && $act== "DELETE") 
		if (!pagepositionsDAL::Pure($mainDB,pagepositionsObj::idFieldName()."=".ToSQLPostVAL($txtID), $errorMsg))
			$pageErro = "Invalid Delete. $error";
	
	if (!isset($SortType) || $SortType=="") $SortType = "ASC";
	if (!isset($SortField) || $SortField=="") $SortField = "webpageID";
	$orderStr = "$SortField $SortType";
	
	// Filter
	$whereFilter = "1=1";
	if (isset($nameSearch) && $nameSearch!="")
		$whereFilter .= " AND pagepositions.name LIKE '%".mysql_escape_string($nameSearch)."%'";
	if (isset($webpageIDSearch) && $webpageIDSearch!="")
		$whereFilter .= " AND webpages.name LIKE '%".mysql_escape_string($webpageIDSearch)."%'";
	if (isset($descriptionSearch) && $descriptionSearch!="")
		$whereFilter .= " AND pagepositions.description LIKE '%".mysql_escape_string($descriptionSearch)."%'";
	if (isset($widthSearch) && $widthSearch!="")
	{
		$args = explode(",", $widthSearch);
		if (count($args)<=1)
			$whereFilter .= " AND pagepositions.width = '".$args[0]."'";
		else
			$whereFilter .= " AND pagepositions.width >= '".$args[0]."' AND pagepositions.width <= '".$args[1]."'";
	}
	if (isset($heightSearch) && $heightSearch!="")
	{
		$args = explode(",", $heightSearch);
		if (count($args)<=1)
			$whereFilter .= " AND pagepositions.height = '".$args[0]."'";
		else
			$whereFilter .= " AND pagepositions.height >= '".$args[0]."' AND pagepositions.height <= '".$args[1]."'";
	}
	if (isset($priceSearch) && $priceSearch!="")
	{
		$args = explode(",", $priceSearch);
		if (count($args)<=1)
			$whereFilter .= " AND pagepositions.price = '".$args[0]."'";
		else
			$whereFilter .= " AND pagepositions.price >= '".$args[0]."' AND pagepositions.price <= '".$args[1]."'";
	}
	
	// Paging
	$rowCount = RowCount($mainDB,  pagepositionsObj::TableName(), $whereFilter, $errorMsg, array('webpages'), array('pagepositions.webpageID=webpages.id'));	
	
	$rowsOfPage	= ROWS_OF_ADMIN_LIST;
	if (!isset($page) || $page<1) $page	= 1;
	$pages	= ceil($rowCount/$rowsOfPage);
	if ($page>$pages && $pages>0) $page = $pages;
	
	$limit = " " .(($page-1)*$rowsOfPage).", ".$rowsOfPage;
	
	$notes = "";
	$paging = Paging($page, $rowCount, $rowsOfPage, $btnNextText, $btnPrevText, $btnGoPageText, "link" /*link class*/, "label" /*link text class*/, "inputNumber" /*input number class*/ , $noted, "document.$frmName");
	
	$returnData = pagepositionsDAL::GetList($mainDB, $whereFilter, $orderStr, $limit, $errorMsg, array('webpages'), array('pagepositions.webpageID=webpages.id'), array('name'));
	
	if ($errorMsg!="" && $pageError=="") $pageError = "Error: ".$errorMsg;	
} 
catch(Exception $ex)
{ $pageError = $ex.message;}
	
?>
<form name=<?php  echo $frmName;?> method=post action="<?php  echo $_SERVER['PHP_SELF']; ?>">
<input type=hidden name=act>
<input type=hidden name=page value="<?php echo $page;?>">
<input type=hidden name=pages value="<?php echo $pages;?>">
<input type=hidden name=txtID>
<input type=hidden name=SortField value="<?php  echo $SortField;?>">
<input type=hidden name=SortType value="<?php  echo $SortType;?>">
<table width="97%" border="0" cellspacing="0" cellpadding="0" align="center">
	<tr valign=middle> 
		<td class="titleText" colspan=3 height=25><?php  echo $title;?></td>
	</tr>
	<?php  
	if ($pageError!="") echo "<tr><td align=center colspan=3 class=\"errorText\">$pageError</td></tr>";
	?>
	<tr> 
		<td align="center" valign="top" colspan=3>
			<TABLE border=0 cellPadding=1 cellspacing="1" width="100%" bgcolor='#ffffff'>
				<tr class='gridHeader'>
					<td width="5%" height="25" nowrap align="center">#</td>
					
<td width="20%" nowrap><a href="#" onClick="_sort('name', document.<?php  echo $frmName;?>);" class="adminGridHeaderLink" title="Sort by position name"><b>Position name (KEY)</b></a></td>
<td width="20%" nowrap><a href="#" onClick="_sort('webpages.name', document.<?php  echo $frmName;?>);" class="adminGridHeaderLink" title="Sort by web site name"><b>Web site name</b></a></td>
<td width="20%" nowrap><a href="#" onClick="_sort('description', document.<?php  echo $frmName;?>);" class="adminGridHeaderLink" title="Sort by description"><b>Description</b></a></td>
<td width="8%" nowrap><a href="#" onClick="_sort('width', document.<?php  echo $frmName;?>);" class="adminGridHeaderLink" title="Sort by width"><b>Width</b></a></td>
<td width="8%" nowrap><a href="#" onClick="_sort('height', document.<?php  echo $frmName;?>);" class="adminGridHeaderLink" title="Sort by height"><b>Height</b></a></td>
<td width="8%" nowrap><a href="#" onClick="_sort('price', document.<?php  echo $frmName;?>);" class="adminGridHeaderLink" title="Sort by price"><b>Price</b></a></td>
					<!--
					<td width="20%" nowrap><a href="#" onClick="_sort('Username', document.<?php  echo $frmName;?>);" class="adminGridHeaderLink" title='Sort by Username'><b>Username</b></a></td>
					<td width="45%" nowrap><a href="#" onClick="_sort('Fullname', document.<?php  echo $frmName;?>);" class="adminGridHeaderLink" title='Sort by Fullname'><b>Fullname</b></a></td>
					<td width="15%" nowrap><a href="#" onClick="_sort('UserType', document.<?php  echo $frmName;?>);" class="adminGridHeaderLink" title='Sort by User type'><b>User type</b></a></td>
					-->
					<td width="15%" nowrap align=center><b>Action</b></td>
				</TR>
				<tr class='gridSearch'>
					<td width="5%" height="20" nowrap align="center">&nbsp;</td>
					<td nowrap>
						<input type=text style="width:100%" name="nameSearch" size="50" maxlength="100" class="inputText" value="<?=$nameSearch?>">
					</td>
					<td nowrap>
						<input type=text style="width:100%" name="webpageIDSearch" size="50" maxlength="100" class="inputText" value="<?=$webpageIDSearch?>">
					</td>
					<td nowrap>
						<input type=text style="width:100%" name="descriptionSearch" size="50" maxlength="100" class="inputText" value="<?=$descriptionSearch?>">
					</td>
					<td nowrap>
						<input type=text style="width:100%" name="widthSearch" size="50" maxlength="100" class="inputText" value="<?=$widthSearch?>">
					</td>
					<td nowrap>
						<input type=text style="width:100%" name="heightSearch" size="50" maxlength="100" class="inputText" value="<?=$heightSearch?>">
					</td>
					<td nowrap>
						<input type=text style="width:100%" name="priceSearch" size="50" maxlength="100" class="inputText" value="<?=$priceSearch?>">
					</td>
					<td width="15%" nowrap align=center>
						<a href="#" onClick="document.<?=$frmName?>.submit();" title="<?php  echo $title_Search;?>" class="buttonlink"><?php  echo $btnSearch;?></a>
					</td>
				</TR>
                <tr style="display:none">
                	<td><INPUT TYPE="submit" NAME="login" VALUE="Search" style="font-family: verdana; font-size:0px; width:0"></td>
                </tr>
			<?php 
			for ($i=0; is_array($returnData) && $i<count($returnData); $i++) {
				$pagepositions = $returnData[$i];
			?>
				<tr class="<?php  if (($i%2)==0) echo "gridOddRow"; else echo "gridEvenRow";?>">
					<td align=right><?php echo sprintf("%5d &nbsp; ", (($page-1)*$rowsOfPage) + $i+1);?></td>
					<td align="left"><?php  echo $pagepositions->name;?></td><td align="left"><?php  echo $pagepositions->otherFields['webpages_name'];?></td><td align="left"><?php  echo $pagepositions->description;?></td><td align="right"><?php  echo $pagepositions->width;?></td><td align="right"><?php  echo $pagepositions->height;?></td><td align="right"><?php  echo $pagepositions->price;?></td>
					<td align=center>
						<a href="#" onClick="_update(<?php  echo $pagepositions->id;?>, '<?php  echo $frmUpdate;?>', document.<?php  echo $frmName;?>);" class="adminGridRowLink" title="<?php  echo $title_Edit;?>">Update</a>
						| 
						<a href="#" onClick="_delete(<?php  echo $pagepositions->id;?>, document.<?php  echo $frmName;?>);" class="adminGridRowLink" title="<?php  echo $title_Delete;?>">Delete</a>
					</td>
				</TR>
			<?php 
			}
			?>
			</TABLE>
		</td>
	</tr>
	<tr valign=bottom> 
		<td colspan=3 height=10></td>
	</tr>
	<tr> 
		<td width="10%">
			<a href="#" onClick="_add('<?php  echo $frmUpdate;?>',document.<?php  echo $frmName;?>);" title="<?php  echo $title_Add;?>" class="buttonlink"><?php  echo $btnAdd_Value;?></a>
		</td>
		<td width="40%" align=right><?php  echo $notes;?></td>
		<td width="50%" class="paging"><?php  echo $paging;?></td>
	</tr>
</table>
</form>
<?php 
include "../footer.php";
?>